package me.wangxuan.pat;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class CustomJTable extends JFrame {
	private JTable table;
	private DefaultTableModel tableModel;

	public CustomJTable() {
		setTitle("Custom JTable Example");
		setSize(600, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());

		// Create table model
		String[] columnNames = { "Column 1", "Column 2", "Button", "Dropdown" };
		tableModel = new DefaultTableModel(columnNames, 0);
		table = new JTable(tableModel);

		// Set up column editors and renderers
		setupTableColumns();

		// Add button to add new rows
		JButton addButton = new JButton("Add Row");
		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Object[] newRow = { "", "", "Click Me", "Option 1" };
				tableModel.addRow(newRow);
			}
		});

		// Add table and button to frame
		add(new JScrollPane(table), BorderLayout.CENTER);
		add(addButton, BorderLayout.SOUTH);

		setVisible(true);
	}

	private void setupTableColumns() {
		table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JTextField()));
		table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new JTextField()));

		// Button column
		table.getColumnModel().getColumn(2).setCellRenderer(new ButtonRenderer());
		table.getColumnModel().getColumn(2).setCellEditor(new ButtonEditor(null));

		// Dropdown column
		JComboBox<String> comboBox = new JComboBox<>(new String[] { "Option 1", "Option 2", "Option 3" });
		table.getColumnModel().getColumn(3).setCellRenderer(new DefaultTableCellRenderer());
		table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBox));
	}

// Button renderer
	static class ButtonRenderer implements TableCellRenderer {
	    private JButton button;

	    public ButtonRenderer() {
	        button = new JButton();
	        button.setPreferredSize(new Dimension(5, 5)); // 设置按钮的首选大小
	        button.setOpaque(true);
	        button.setFocusPainted(false); // 可选，如果你不想显示按钮的边框
	    }

	    @Override
	    public Component getTableCellRendererComponent(JTable table, Object value,
	                                                boolean isSelected, boolean hasFocus,
	                                                int row, int column) {
	        if (value instanceof String) {
	            button.setText((String) value);
	            button.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
	            button.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
	        }
	        // 设置按钮的大小
	        button.setPreferredSize(new Dimension(23, 23)); // 宽度80像素，高度23像素
	        return button;
	    }
	}

// Button editor
	static class ButtonEditor extends AbstractCellEditor implements TableCellEditor {
	    private final JButton button;
	    private String text;
	    private final ActionListener actionListener;

	    public ButtonEditor(ActionListener actionListener) {
	        this.actionListener = actionListener;
	        button = new JButton();
	        button.addActionListener(actionListener);
	        button.setFocusPainted(false);
	    }

	    @Override
	    public Component getTableCellEditorComponent(JTable table, Object value,
	                                              boolean isSelected, int row, int column) {
	        if (value instanceof String) {
	            text = (String) value;
	            button.setText(text);
	            button.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
	            button.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
	        }
	        button.setPreferredSize(new Dimension(23, 23)); // 设置按钮大小
	        return button;
	    }

	    @Override
	    public Object getCellEditorValue() {
	        return text;
	    }
	}

	public static void main(String[] args) {
		try {
			System.out.println(UIManager.getSystemLookAndFeelClassName());
		    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
		    e.printStackTrace();
		}
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new CustomJTable();
			}
		});
	}
}
